/*
 * StatusLayer.h
 *
 *  Created on: 2010-9-4
 *      Author: try
 */

#ifndef STATUSLAYER_H_
#define STATUSLAYER_H_

#include "../../commons.h"
#include "../../gdiplus/gdraw.h"

using namespace std;
using namespace Ed;
using namespace Thk;

namespace S {
  class WorkPage;
  class StatusItem;
  /**
   * 状态栏，显示当前鼠标在工作层上的坐标, 工作层中的元素数量，重画的元素数量
   */
  class StatusLayer: public Layer {
    public:
      StatusLayer(WorkPage* page):Layer(page){
        coordinate = new StatusItem(0, 0, 110, 30);
        elementNums = new StatusItem(120, 0, 100, 30);
        drawedCount = new StatusItem(220, 0, 150, 30);
        spendTime =  new StatusItem(350, 0, 250, 30);
        addElement(coordinate);
        addElement(elementNums);
        addElement(drawedCount);
        addElement(spendTime);

        showCoordinate(0, 0);
        showElementNums(0);
        showDrawedCount(0);
        showRefreshSpendTime(0);
      }
      virtual ~StatusLayer(){
        delete coordinate;
        delete elementNums;
        delete drawedCount;
        delete spendTime;
      }

      void showCoordinate(real x, real y){
        swprintf(text, L"Coord:%d,%d", (int)x, (int)y);
        coordinate->setText(text);
      }

      void showElementNums(int nums){
        swprintf(text, L"Shapes:%d", nums);
        elementNums->setText(text);
      }

      void showDrawedCount(int nums){
        swprintf(text, L"Refresh Count:%d", nums);
        drawedCount->setText(text);
      }

      void showRefreshSpendTime(float usetime){
        wchar_t text[50];
        swprintf(text, L"Refresh Spend time: %.2f mills", usetime);
        spendTime->setText(text);
      }

    private:
      /**
       * 背景矩形边框
       */
      class StatusItem: public Element {
        public:
          StatusItem(real x, real y, real w, real h){
            Gdiplus::FontFamily fontName( L"Courier New" );
            font = new Gdiplus::Font( &fontName, 12, Gdiplus::FontStyleRegular, Gdiplus::UnitPixel );
            brush = new Gdiplus::SolidBrush( Gdiplus::Color( 255, 255, 255, 255 ));
            format.SetLineAlignment(Gdiplus::StringAlignmentCenter);
            setWidth(w);
            setHeight(h);
            setCoordinate(x, y);
            createBoundRect();
          }
          virtual ~StatusItem(){
            delete brush;
            delete font;
          }

          void setText(std::wstring& text){
            this->text = text;
            setModified(true);
          }

          void setText(wchar_t* text){
            this->text = text;
            setModified(true);
          }

          /**
           * 画此元素
           */
          void draw(Gdiplus::Graphics* g) {
            g->DrawString(text.c_str(), text.size(), font, *getBoundRect(), &format, brush);
            setModified(false);
            setRedraw(false);
          }

//          inline const Gdiplus::RectF* getBoundRect() {
//            if (!boundRect) {
//              //boundRect = new Gdiplus::RectF(x()+adjustVal, y()+adjustVal, width+adjustVal*2 + 1, height+adjustVal*2 + 1);
//              boundRect = new Gdiplus::RectF(x(), y(), width + 1, height + 1);
//            }
//            return boundRect;
//          }

        private:
//          real width, height;
          std::wstring text;
          Gdiplus::SolidBrush* brush;
          Gdiplus::Font* font;
          Gdiplus::StringFormat format;
      };

      wchar_t text[50];
      //鼠标在工作层上的坐标
      StatusItem* coordinate;
      //元素数量
      StatusItem* elementNums;
      //重绘元素数量
      StatusItem* drawedCount;
      //刷新工作层使用时间
      StatusItem* spendTime;
  };
}
#endif /* STATUSLAYER_H_ */
